01-26-24 (Friday)

From the lectionary reading: Psalm 111

1 Announcements

  • Comment on some code written by students

2 Quiz 1 on Moodle

  • Go to “Quiz 1” in our Moodle page. We’ll do it in class.

3 Example: Bhaskara’s formula

  • Is the following code right? What’s wrong with it?
from math import sqrt

a = int(input())
b = int(input())
c = int(input())

x1 = ( -b + sqrt(b^2 - 4ac) ) / 2a
x2 = ( -b - sqrt(b^2 - 4ac) ) / 2a

4 Example: Coding text

  • Suppose I want to print the following string:

Gandalf said: “You shall not pass!”

  • Why does the following code doesn’t work? What can I do to fix it?
print(""Gandalf said: "You shall not pass!"")
  • Now, suppose I want to print a poem spanning multiple lines. How can I do that?

Over hill, over dale,
Thorough bush, thorough brier,
Over park, over pale,
Thorough flood, thorough fire!

Tip: check for Python’s escape characters

5 Discussion: computers are actually stupid…?

Q: “If computers are so smart, why don’t they program themselves?” A: “Somebody would first have to write the program, and no one has yet been that smart”. - Derek Robinson, “Functions” (in: Software Studies, a Lexicon)

  • Writing code requires us to be really precise and non-ambiguous. It is really nitpicky and doesn’t allow even little inaccuracies (which humans can easily disregard…)
    • Sometimes what we write makes sense to us, but it doesn’t “make sense” to the computer.
    • Sometimes it doesn’t work and it won’t even show the error!
    • Sometimes it “works”, but I doesn’t do exactly what we are thinking it should do… (an illusion of achievement).
  • Solutions?

Opinion: Amy Ko, Critically Conscious Computing

These efforts to make communication with computers easier, alas, have not resulted in programming being easy, creating a broad cultural belief that programming is inaccessible, hard to learn, and only for”smart” people. In reality, however, programming languages are often poorly designed, poorly explained, and poorly taught, and many of their users gatekeep access to them, reinforcing elitist, essentialist attitudes about who is “smart” enough to use them.

6 Comments

You can add a comment to a single line:

# Comment on a single line
name = "Pied Piper" # Comment after code

Or, when commenting in multiple lines:

# This is a comment written over
# more than one line
# simply using multiple '#'s

print("Hello, World!")

or

"""
This is a string written over
more than one line
"""

print("Hello, World!")

This last case just “states” a multi-line string that will not be printed if just provided in the middle of the code. Therefore it works as a kind of comment.

Tip: enabling and disabling parts of the code

A common practice in programming is adding comment tags (#) to parts of the code you want to temporarily not execute, but don’t want to delete completely. Speaking rigorously, you shouldn’t do that because that is not the purpose of comments, however, it still helps a lot! Many IDEs, furthermore, have buttons to enable and disable comments given some selected parts of code.

For example, suppose you don’t want to ask the user for input and just test with some fixed number (goes faster when executing code for tests):

# x = int(input("Please enter your age: "))
x = 18
print("Your age is", x)

If you want to “toggle” again user input, just un-comment the input line and comment (or delete) the x = 18 line!

For more detail, check this guide. (Later in the course we will go through the docstring stuff for documenting user defined functions.)

7 Programming Style

  • Like in any language, style and form makes a BIG difference. One could say that it doesn’t matter once it works, however, that is a very narrow way to understand human expression…

  • Sometimes there are multiple ways to write code that works, but many of them are not be hospitable. As an example:

prime_numbers = [x for x in range(2, 101) if all(x % y != 0 for y in range(2, int(x**0.5) + 1))

This is very concise, however, it is not readable. It is a monster of an expression!

What would happen if we just rewrite it like this?

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

prime_numbers = [x for x in range(2, 101) if is_prime(x)]

It is more code, however, it is much more readable… (Don’t care about not understanding all the syntax now, we’ll see that later).

  • Some programming style tips:
    • Lack of comments is a problem. But too much commenting is also a problem.
    • Bad naming of variables and functions: for example, abstract names as “variable1”, or names that don’t reflect exactly what is their purpose.
    • Spacing usually doesn’t matter for the working of code, but it can be a problem for reading.
    • Follow community standards. Read other people’s code and learn. Develop a common understanding.
      • Isn’t it good to find yourself in known territory, with same languages, habits, food?
      • An interesting question: could there be some “strange” habits in Python programming for people with cultural backgrounds different from American, white and men?

Python has a nice style guide available. It is good to consult it once in a while.

8 Discussion: Hospitable code

Professor Victor Norman, from Calvin, wrote a nice article calling attention to an important practice: we should always aim to write hospitable code.

You should read the whole article when you have time, but just to summarize some of the points:

  1. Not only the function, but also the form of the code matters, to me and to God.
  2. A common saying in the software development community: “code is written once, but read a thousand times”.
  3. Therefore, because you care for your neighbor, you should serve them by making your code hospitable: “code that welcomes the reader to come in and be comfortable, to enjoy the cleanliness of the code, to feel at home, and to see that the space has been carefully prepared with guests in mind.”

8.1 Hospitable code in the scientific and engineering community?

There is a common perception that software written for scientific and engineering communities are the worst documented and organized ever. People usually think that since we are dealing with experts, it would be a waste of time to just make things clean and organized.

  • Thus, reflect: as Christians, is it really a waste of time to make things beautiful and welcoming? Or is it just getting to final result that matters?